<?php

class OnprocessorderController extends BaseController {
	//on process oerder
		public function getOnprocessorder()
	{	
		Session::forget('list');
		Session::forget('filter');
		Session::forget('filterby');
		Session::forget('keyword');
		Session::forget('dateby');
		Session::forget('datestart');
		Session::forget('dateend');
		$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);

		return View::make('admin.order.onprocess.onprocessorder')->with('onprocessorder',$onprocessorder);
	}

	public function postSetduedate($id)
	{
		$duedate = order::find($id);
		$duedate->order_due_date = date("Y-m-d h:m:s",strtotime(Input::get('duedate')));
		$duedate->save();


		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Set due date order : ".$id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/order/onprocessorder')->with('message','Tanggal jadi telah ditentukan');

	}

	//=================================
	public function postOnprocessorderfinished()
	{
		$orders = DB::table('sys_order')
				->join('sys_customer','customer_id','=','order_customer_id')
				->get();
		$a = 0;
		foreach($orders as $order){
		
		if(Input::get($order->order_id)=='1'){
			$a = $a + Input::get($order->order_id);
			if($order->order_due_date==''){
				return Redirect::to('/order/onprocessorder')->with('error','Tanggal jadi belum ditentukan');
				}

			$progress = DB::table('sys_order')
				->leftjoin('sys_order_progress','order_progress_order_id','=','order_id')
				->where('order_id',$order->order_id)
				->orderBy('order_id')
				->groupBy('order_id')
				->first();
			$total = DB::table('sys_order_progress')
					->where('order_progress_order_id',$order->order_id)
					->first();
			$outstanding = $progress->order_qty-$total->order_progress_qty_finished;
			if($outstanding<>0){
				return Redirect::to('/order/onprocessorder')->with('error','Pesanan belum selesai ');
			}
		
			$orders = DB::table('sys_order')
					->where('order_id','=',$order->order_id)
					->update(array('order_status' => 'Finished'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $order->order_id;
			$orderstatus->order_status_type = 'Finished';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set order : ".$order->order_id." Finished";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			$user = array(
			  'email'=>$order->customer_email,
			  'nama'=>$order->customer_name
			);
			//send mail
			Mail::send('emails.pesananfinish', array('pesan'=>'Pesanan anda telah selesai kami kerjakan.','ordercode'=>$order->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Pesanan anda telah selesai');
	    	});

		}

		}
		if($a<>0){
			return Redirect::to('/order/onprocessorder')->with('message','Order telah selesai diproses');
		}
		return Redirect::to('/order/onprocessorder')->with('error','Silahkan pilih order');
		
	}

	public function getOnprocessorderfinishedid($id)
	{
			$orders = DB::table('sys_order')
				->join('sys_customer','customer_id','=','order_customer_id')
				->where('order_id','=',$id)
				->get();
			foreach($orders as $order){
			if($order->order_due_date==''){
				return Redirect::to('/order/onprocessorder')->with('error','Tanggal jadi belum ditentukan');
			}


			$progress = DB::table('sys_order')
				->leftjoin('sys_order_progress','order_progress_order_id','=','order_id')
				->where('order_id',$id)
				->orderBy('order_id')
				->groupBy('order_id')
				->first();
			$total = DB::table('sys_order_progress')
					->where('order_progress_order_id',$id)
					->first();
			$outstanding = $progress->order_qty-$total->order_progress_qty_finished;
			if($outstanding<>0){
				return Redirect::to('/order/onprocessorder')->with('error','Pesanan belum selesai ');
			}




			$orders = DB::table('sys_order')
					->where('order_id','=',$id)
					->update(array('order_status' => 'Finish'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $id;
			$orderstatus->order_status_type = 'Finished';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set order : ".$id." Finished";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			$user = array(
			  'email'=>$order->customer_email,
			  'nama'=>$order->customer_name
			);
			//send mail
			Mail::send('emails.pesananfinish', array('pesan'=>'Pesanan anda telah selesai kami kerjakan.','ordercode'=>$order->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Pesanan anda telah selesai');
	    	});
		}

		return Redirect::to('/order/onprocessorder')->with('message','Order telah selesai');
	}

	public function postOnprocessordercancel()
	{
		$orders = DB::table('sys_order')
				->get();
		foreach($orders as $order){
		if(Input::get($order->order_id)=='1'){
			$orders = DB::table('sys_order')
					->where('order_id','=',$order->order_id)
					->update(array('order_status' => 'Approve'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $order->order_id;
			$orderstatus->order_status_type = 'Approve';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set cancel order : ".$order->order_id." Approve";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();
		}
		}
		return Redirect::to('/order/onprocessorder')->with('message','Order telah dicancel dari daftar proses');
	}

	public function getOnprocessordercancelid($id)
	{
			$orders = DB::table('sys_order')
					->where('order_id','=',$id)
					->update(array('order_status' => 'Approve'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $id;
			$orderstatus->order_status_type = 'Approve';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set cancel order : ".$id." On Approve";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

		return Redirect::to('/order/onprocessorder')->with('message','Order telah dicancel dari daftar proses');
	}
	//===================================

	public function postOnprocessorder()
	{	

		if(Input::get('filter')=='date'){
			Session::forget('list');
			Session::forget('filterby');
			Session::forget('keyword');
			Session::put('filter','date');
				if(Input::get('datestart')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
				if(Input::get('dateend')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
			Session::put('datestart',Input::get('datestart'));
			Session::put('dateend',Input::get('dateend'));
			Session::put('dateby',Input::get('dateby'));

			if(Input::get('dateby')=='all'){
			$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else if(Input::get('dateby')=='dateorder'){
			$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->where('order_status_type','=','Pending')
						->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else if(Input::get('dateby')=='dateapprove'){
			$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->where('order_status_type','=','Approve')
						->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else if(Input::get('dateby')=='dateprocess'){
			$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->where('order_status_type','=','On Process')
						->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}
			return View::make('admin.order.onprocess.onprocessorder')->with('onprocessorder',$onprocessorder);
		}else if(Input::get('filter')=='type'){
			Session::forget('filterby');
			Session::forget('keyword');
			Session::forget('dateby');
			Session::put('filter','type');
			Session::put('list', Input::get('typeby'));
			if(Input::get('typeby')=='all'){
			$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else{
			$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->where('order_name','=',Input::get('typeby'))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}
			return View::make('admin.order.onprocess.onprocessorder')->with('onprocessorder',$onprocessorder);
		}else if(Input::get('filter')=='keyword'){
			Session::put('filter','keyword');
			Session::forget('list');
			Session::forget('dateby');
			Session::put('filterby',Input::get('filterby'));
			Session::put('keyword',Input::get('keyword'));
			if(Input::get('filterby')=='code'){
			$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->where('order_code','LIKE','%'.Input::get('keyword').'%')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			return View::make('admin.order.onprocess.onprocessorder')->with('onprocessorder',$onprocessorder);
		}else if(Input::get('filterby')=='name'){
			$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->where('customer_name','LIKE','%'.Input::get('keyword').'%')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			return View::make('admin.order.onprocess.onprocessorder')->with('onprocessorder',$onprocessorder);
		}
		}else{
        	if(Session::get('filter')=="date"){
        			if(Session::get('dateby')=='all'){
					$onprocessorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','On Process')
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='orderdate'){
					$onprocessorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','On Process')
								->where('order_status_type','=','Pending')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='orderapprove'){
					$onprocessorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','On Process')
								->where('order_status_type','=','Approve')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='orderprocess'){
					$onprocessorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','On Process')
								->where('order_status_type','=','On Process')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}
        	}else if(Session::get('filter')=="type"){
        				if(Session::get('list')=='all'){
					$onprocessorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','On Process')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}else{
					$onprocessorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','On Process')
									->where('order_name','=',Session::get('list'))
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}
        	}else if(Session::get('filter')=="keyword"){
        			if(Session::get('filterby')=='code'){
						$onprocessorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','On Process')
									->where('order_code','LIKE','%'.Session::get('keyword').'%')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}else if(Session::get('filterby')=='name'){
						$onprocessorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','On Process')
									->where('customer_name','LIKE','%'.Session::get('keyword').'%')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}
        	}
        }
		return View::make('admin.order.onprocess.onprocessorder')->with('onprocessorder',$onprocessorder);
		



	}

	public function getDetailonprocessorder($id)
	{	$order = DB::table('sys_order')
					->where('order_id','=',$id)
					->first();
		if($order->order_name=='idcard'){
		$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_idcard','order_idcard_order_id','=','order_id')
						->join('ref_idcard_type','order_idcard_idcard_type_id','=','idcard_type_id')
						->join('ref_idcard_quality','order_idcard_idcard_quality_id','=','idcard_quality_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->where('order_id','=',$id)
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->first();
		$detailorder =  DB::table('sys_order')
						->join('sys_order_idcard','order_idcard_order_id','=','order_id')
						->join('sys_order_idcard_detail','order_idcard_detail_order_idcard_id','=','order_idcard_id')
						->join('ref_additional_features','order_idcard_detail_idcard_additional_features_id','=','additional_features_id')
						->where('order_status','=','On Process')
						->where('order_id','=',$id)
						->get();
		return View::make('admin.order.onprocess.detailonprocessorderidcard', compact('onprocessorder',$onprocessorder,'detailorder',$detailorder));
	}else if($order->order_name=='packaging'){

	$onprocessorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_packaging','order_packaging_order_id','=','order_id')
						->join('sys_order_packaging_detail','order_packaging_detail_order_packaging_id','=','order_packaging_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','On Process')
						->where('order_id','=',$id)
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->first();
		$detailorder =  DB::table('sys_order')
						->join('sys_order_packaging','order_packaging_order_id','=','order_id')
						->join('sys_order_packaging_detail','order_packaging_detail_order_packaging_id','=','order_packaging_id')
						->join('ref_additional_features','order_packaging_detail_additional_features_id','=','additional_features_id')
						->where('order_status','=','On Process')
						->where('order_id','=',$id)
						->get();
		return View::make('admin.order.onprocess.detailonprocessorderpackaging', compact('onprocessorder',$onprocessorder,'detailorder',$detailorder));
	}else if($order->order_name=='brosur'){
		echo "brosur";
	}else if($order->order_name=='nota'){
		echo "nota";
	}else if($order->order_name=='kaos'){
		echo "kaos";
	}

	}

	public function getComment($id)
	{
		$comment = DB::table('sys_customer')
				->join('sys_order','order_customer_id','=','customer_id')
				->where('order_id',$id)
				->first();


		$comments = DB::table('sys_order_comment')
				->join('sys_admin','order_comment_admin_id','=','id')
				->where('order_comment_order_id',$id)
				->get();
		return View::make('admin.order.onprocess.commentonprocessorder',compact('comment',$comment,'comments',$comments));
	}

	public function postAddcomment($id)
	{
		$comment = New comment();
		$comment->order_comment_admin_id = Session::get('admin_id');
		$comment->order_comment_order_id = $id;
		$comment->order_comment_desc = Input::get('comment');
		$comment->order_comment_date = date('Y-m-d H:i:s');
		$comment->save();


		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Add comment order : ".$id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/order/onprocessorder/comment/'.$id)->with('message','Keterangan berhasil ditabahkan');

	}

	public function postDeletecomment($id)
	{
		$comments = DB::table('sys_order_comment')
				->get();
		foreach($comments as $comment){
		
			if(Input::get($comment->order_comment_id)=='1'){
			$comment = comment::find($comment->order_comment_id);
			$comment->delete();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Delete comment order : ".$id;	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();
			}
		}

		return Redirect::to('/order/onprocessorder/comment/'.$id)->with('message','Keterangan berhasil ditabahkan');

	}

	public function getProgress($id)
	{
		$progress = DB::table('sys_order')
				->leftjoin('sys_order_progress','order_progress_order_id','=','order_id')
				->where('order_id',$id)
				->orderBy('order_id')
				->groupBy('order_id')
				->first();


		$total = DB::table('sys_order_progress')
				->where('order_progress_order_id',$id)
				->first();

		$detailprogress = DB::table('sys_order_progress_log')
				->where('order_progress_log_order_id',$id)
				->get();



		 return View::make('admin.order.onprocess.progressonprocessorder',compact('detailprogress',$detailprogress,'progress',$progress,'total',$total));
	}

	public function postAddprogress($id)
	{
		
		if((Input::get('finished')=='') AND (Input::get('delivered')=='')){
			return Redirect::to('/order/onprocessorder/progress/'.$id)->with('error','Harap isikan jumlah selesai atau jumlah terkirim');
		}

		if(Input::get('finished')<>''){
		$progresslog = New progresslog;
		$progresslog->order_progress_log_order_id = $id;
		$progresslog->order_progress_log_qty = Input::get('finished');
		$progresslog->order_progress_log_status = 'Finished';
		$progresslog->order_progress_log_date = date('Y-m-d H:i:s');
		$progresslog->save();

		$progressfinished = DB::table('sys_order_progress')
					->where('order_progress_order_id','=',$id)
					->first();
		$qtyfinish = $progressfinished->order_progress_qty_finished+Input::get('finished');
		$progressfinish = DB::table('sys_order_progress')
					->where('order_progress_order_id','=',$id)
					->update(array('order_progress_qty_finished' => $qtyfinish));	

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Add progress finished order : ".$id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		}
		if(Input::get('delivered')<>''){
		$progresslog = New progresslog;
		$progresslog->order_progress_log_order_id = $id;
		$progresslog->order_progress_log_qty = Input::get('delivered');
		$progresslog->order_progress_log_status = 'Delivered';
		$progresslog->order_progress_log_date = date('Y-m-d H:i:s');
		$progresslog->save();

		$progressdelivered = DB::table('sys_order_progress')
					->where('order_progress_order_id','=',$id)
					->first();
		$qtydelivered = $progressdelivered->order_progress_qty_delivered+Input::get('delivered');
		$progressdelivered = DB::table('sys_order_progress')
					->where('order_progress_order_id','=',$id)
					->update(array('order_progress_qty_delivered' => $qtydelivered));	

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Add progress delivered order : ".$id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		}

	return Redirect::to('/order/onprocessorder/progress/'.$id)->with('message','Jumlah selesai dan/atau jumlah terkirim berhasil ditabahkan');
	}


	public function postDeleteprogress($id)
	{
		$progresss = DB::table('sys_order_progress_log')
				->get();
		foreach($progresss as $progress){
		
			if(Input::get($progress->order_progress_log_id)=='1'){
			$progressall = progresslog::find($progress->order_progress_log_id);
				if($progress->order_progress_log_status=="Finished"){
					$progressfinished = DB::table('sys_order_progress')
					->where('order_progress_order_id','=',$id)
					->first();
					$qtyfinish = $progressfinished->order_progress_qty_finished-$progress->order_progress_log_qty;
					$progressfinish = DB::table('sys_order_progress')
								->where('order_progress_order_id','=',$id)
								->update(array('order_progress_qty_finished' => $qtyfinish));					
				}else if($progress->order_progress_log_status=="Delivered"){
					$progressdelivered = DB::table('sys_order_progress')
					->where('order_progress_order_id','=',$id)
					->first();
					$qtydelivered = $progressdelivered->order_progress_qty_delivered-$progress->order_progress_log_qty;
					$progressdelivered = DB::table('sys_order_progress')
								->where('order_progress_order_id','=',$id)
								->update(array('order_progress_qty_delivered' => $qtydelivered));					
				}
			$progressall->delete();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Delete progress order : ".$id;	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();
			}
		}

		return Redirect::to('/order/onprocessorder/progress/'.$id)->with('message','Keterangan berhasil dihapus');

	}
}
